//
//  TRListTableViewController.m
//  WeiMiLan
//
//  Created by cong on 14-7-20.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRListTableViewController.h"
#import "RankListCell.h"
#import "PullTableView.h"
@interface TRListTableViewController ()<PullTableViewDelegate>
@property (weak, nonatomic) IBOutlet PullTableView *tableView;
@property(nonatomic,strong)NSMutableArray* listArry;
@property(nonatomic,strong)NSMutableArray* idArry;

@property(nonatomic,assign)int  pageNum;
@property(nonatomic,assign)int  oldCount;

@property(nonatomic)DirectorySelect currentSeletList;
@property(strong,nonatomic)NSDictionary * pageDict;
@end

@implementation TRListTableViewController

-(void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView{
    pullTableView.pullTableIsRefreshing=YES;
    
    [self updateString:self.currentSeletList andPage:1 andIDs:nil];
    [self performSelector:@selector(reflushNow) withObject:nil afterDelay:0.3];
}

-(void)reflushNow{
    self.tableView.pullTableIsRefreshing=NO;
    self.tableView.pullTableIsLoadingMore= NO;
}

- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView{
    
    pullTableView.pullTableIsLoadingMore= YES;
    
    if (self.pageDict) {
        if ([self.pageDict[@"NUMBER"] intValue] == [self.pageDict[@"PAGES"] intValue]) {
            [SVProgressHUD showImage:nil status:@"已无更多"];
            return;
        }
    }
    
    [self updateString:self.currentSeletList andPage:++self.pageNum andIDs:nil];
    [self performSelector:@selector(reflushNow) withObject:nil afterDelay:0.3];
}

#pragma mark-----请求数据

-(void)updateString:(DirectorySelect)diret andPage:(int)pageNum andIDs:(NSArray*)ids{
    
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    int uid = [userInfo ql_intForKey:@"ID"];
    
    [ApplicationDelegate.webAPI getWebAPIWithParams:
     @{
       @"userId":QLIntToNumber(uid),
       TYPE:[NSString stringWithFormat:@"%d",diret],
       PAGE_NUM:[NSString stringWithFormat:@"%d",pageNum],
       PRODUCT_TYPE_ORDER_FIELD:DIR_ORDER_FIELD_CREATEDATE,
       PRODUCT_TYPE_ORDER_DIRECTION:DIR_ORDER_DIRECTION_ASC
       
       } andMethodNode:NODE_DIRECTORY completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
           
           NSString * message = responseObject[@"MESSAGE_INFO"][@"MESSAGE"];
           
           if ( [responseObject[@"MESSAGE_INFO"][@"CODE"] isEqualToString:@"0000"]) {
               if (pageNum==1) {
                   self.listArry=[NSMutableArray array];
               }
               self.pageDict = responseObject[@"PAGE_INFO"];
               NSArray *a=responseObject[@"PACKAGE_RSP"][@"PACKAGE"];
               [self.listArry addObjectsFromArray:a];
               [self.tableView reloadData];
               [self reflushNow];
           }else{
               [SVProgressHUD showImage:nil status:message];
           }
       }];
    
    
}


#pragma mark-----切换请求
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.listArry = [@[]mutableCopy];
    self.idArry = [@[]mutableCopy];
    self.tableView.pullDelegate = self;
    
    self.tableView.pullBackgroundColor = [UIColor whiteColor];
    self.tableView.pullTextColor = [UIColor blackColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"RankListCell" bundle:nil] forCellReuseIdentifier:@"RankListCell"];
   
    self.pageNum = 1;
    
    [self initNavigation];
    self.currentSeletList = DirectoryCollection;
    switch (self.usage) {
        case myCollVC:
            self.currentSeletList = DirectoryCollection;
            break;
        case sharedVC:
            self.currentSeletList = DirectoryShareBillboard;
            break;
            
        case collectedVC:
            self.currentSeletList = DirectoryCollectionBillboard;
            break;
        case downVC:
            self.currentSeletList = DirectoryDownload;
            break;
    }
    [self updateString:self.currentSeletList andPage:self.pageNum andIDs:nil];
    self.tableView.pullTableIsRefreshing = YES;
}

- (void)initNavigation
{
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view addSubview:self.weChatNavigationBar];
    self.weChatNavigationBar.titleLabel.text=@"排行榜";
    [self.weChatNavigationBar.rightButton setImage:nil forState:0];
    [self.weChatNavigationBar .leftButton addTarget:self action:@selector(exit)  forControlEvents:UIControlEventTouchUpInside];
    self.weChatNavigationBar.titleLabel.width=80;
    self.weChatNavigationBar.titleLabel.top=30;
    self.weChatNavigationBar.titleLabel.font=[UIFont fontWithName:nil size:14.0f];
    self.weChatNavigationBar.titleLabel.right=320;
    
    
    NSArray *segArray=@[@"分享",@"收藏",@"下载"];
    UISegmentedControl *seg=[[UISegmentedControl alloc] initWithItems:segArray];
    seg.frame=CGRectMake(78, 33, 160, 27);
    seg.tintColor=[UIColor purpleColor];
    seg.backgroundColor=[UIColor whiteColor];
    seg.segmentedControlStyle = UISegmentedControlStyleBordered;
    seg.layer.borderColor=[UIColor whiteColor].CGColor;
    seg.layer.borderWidth=1.0f;
    seg.layer.cornerRadius=5.0f;
    seg.clipsToBounds=YES;
    
    [seg addTarget:self action:@selector(changV:) forControlEvents:UIControlEventValueChanged];
    
    seg.selectedSegmentIndex=0;
    [self.weChatNavigationBar addSubview:seg];
    
}

-(void)exit
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark-----切换排行榜
-(void)changV:(UISegmentedControl *)s
{
    
    self.usage=s.selectedSegmentIndex+1;
    switch (s.selectedSegmentIndex) {
        case 0:
            self.currentSeletList = DirectoryShareBillboard;
            break;
        case 1:
            self.currentSeletList = DirectoryCollectionBillboard;
            break;
        case 2:
            self.currentSeletList = DirectoryDownload;
            break;
            
        default:
            break;
    }
    self.tableView.pullTableIsRefreshing = YES;
    [self pullTableViewDidTriggerRefresh:nil];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArry.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RankListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankListCell" forIndexPath:indexPath];
    
    
    cell.kindLabel.text =self.listArry[indexPath.row][@"BRAND_NAME"];
    cell.nameLabel.text =self.listArry[indexPath.row][@"NAME"];
    
    cell.timesLabel.text = self.listArry[indexPath.row][@"COUNT"];
    
    [cell.HeadImageV setImageWithURL:[NSURL URLWithString:self.listArry[indexPath.row][@"THUMBNAIL_URL"]] placeholderImage:[UIImage imageNamed:@"timeline_image_loading"]];
    
    UIImage * image;
    
    switch (self.usage) {
            
        case myCollVC:
            
            image = [UIImage imageNamed:@"coll收藏按钮"];
            break;
            
        case sharedVC:
            
            image = [UIImage imageNamed:@"朋友圈"];
            break;
            
        case collectedVC:
            image = [UIImage imageNamed:@"coll收藏按钮"];
            
            
            break;
        case downVC:
            image = [UIImage imageNamed:@"down下载按钮"];
            break;
    }
    cell.smallIv.image = image;
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 95.f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
   // NSLog(@"mmmmmm:%@",self.listArry);
    
    TRProductDetailViewController *vc = [[TRProductDetailViewController alloc] init];
    vc.productTypeId = self.listArry[indexPath.row][@"ID"];
    [self presentViewController:vc animated:YES completion:nil];
}


@end
